import axios from 'axios'
import qs from 'qs'
import {
    MessageBox,
    Message
} from 'element-ui'
import store from '@/store'
import {
    getAuth
} from '@/utils/auth'


let preUrl = process.env.VUE_APP_BASE_API
// create an axios instance
axios.create({
    timeout: 5000 // request timeout
})

// request interceptor
axios.interceptors.request.use(
    config => {
        // do something before request is sent
        if (store.getters.token) {
          config.headers['TOKEN'] = getAuth('T')
        }
        return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
axios.interceptors.response.use(
    response => {
        // 未设置状态码则默认成功状态
        const code = response.data.code || 200;
        if (code === '401') {
            MessageBox.confirm('登录状态已过期，您可以继续留在该页面，或者重新登录', '系统提示', {
                confirmButtonText: '重新登录',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                localStorage.setItem("username",'');
                store.dispatch('user/logout').then(() => {
                    location.reload()
                })
            }).catch(() => {

            });
            return Promise.reject('无效的会话，或者会话已过期，请重新登录。')
        }else {
            return response
        }
    },
    error => {
        /* let { message } = error;
        Message({
            message: message,
            type: 'error',
            duration: 5 * 1000
        }) */
        return Promise.reject(error)
        /* let{status,message} = error.response.data;
        if(status == '500'){
            Message.error(message);
            localStorage.setItem("username",'');
            store.dispatch('user/logout');
        }
        return Promise.reject(error) */
    }
)



/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const get = (url, params) => {
    return new Promise((resolve, reject) => {
        axios.get(preUrl+url, {
            params: params
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err)
        })
    });
}



/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export const post = (url, params, toQs) => {
    return new Promise((resolve, reject) => {
        /* 是否序列化 */
        if (toQs !== 'notQs') {
            params = qs.stringify(params);
        }
        axios.post(preUrl+url, params).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err)
        })
    });
}

export const postExcel = (url, params, toQs) => {
    return new Promise((resolve, reject) => {
        /* 是否序列化 */
        if (toQs !== 'notQs') {
            params = qs.stringify(params);
        }
        axios({method:'post',url:preUrl+url, data:params,responseType:'blob'}).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err)
        })
    });
}
export const getExcel = (url, params, toQs) => {
    return new Promise((resolve, reject) => {
        /* 是否序列化 */
        if (toQs !== 'notQs') {
            params = qs.stringify(params);
        }
        axios({method:'get',url:preUrl+url, data:params,responseType:'blob'}).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err)
        })
    });
}


